package com.atguigu.educenter.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.ordervo.UcenterMemberOrder;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.RegisterVo;
import com.atguigu.educenter.mapper.UcenterMemberMapper;
import com.atguigu.educenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-10-17
 */
@RestController
@RequestMapping("/educenter/member")
//@CrossOrigin
public class UcenterMemberController {
//登录
    @Autowired
    private UcenterMemberService memberService;
    @PostMapping("login")
    public R login(@RequestBody UcenterMember member){
        //返回一个string，采用token的方式存储
        String token=memberService.loin(member);
        return R.ok().data("token",token);
    }
    //注册
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo){
        memberService.register(registerVo);
        return R.ok();
    }
    //解析token，根据token获取对应的id
    @GetMapping("getMenberInfo")
    public R getMenberInfo(HttpServletRequest request){
        //调用jwt工具类，获取token的信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //根据id查询用户信息
        UcenterMember member = memberService.getById(memberId);
        return R.ok().data("userInfo",member);
    }

//    //根据token字符串获取用户信息
////    @PostMapping("getInfoUc/{id}")
////    public UcenterMember getInfo(@PathVariable String id) {
//////根据用户id获取用户信息
////        UcenterMember ucenterMember = memberService.getById(id);
////        UcenterMember memeber = new UcenterMember();
////        RegisterVo registerVo = new RegisterVo();
////        BeanUtils.copyProperties(ucenterMember,memeber);
////        return memeber;
////    }

    //根据id获取用户信息
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id){
        UcenterMember member = memberService.getById(id);
        //赋值对象
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,ucenterMemberOrder);
//        System.out.println("老师信息："+ucenterMemberOrder);
        return ucenterMemberOrder;
    }
    //查询某一天注册人数
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day){
        Integer count=memberService.countRegisterDay(day);
        return R.ok().data("countRegister",count);
    }
}

